home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 833 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP! File Pointers
  5. Date: 9 Jan 1996 12:25:54 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4cu8f2$c40@umbc9.umbc.edu>
  8. References: <4csr4c$fem@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: f-umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Roberino <roberino@aol.com> wrote:
  13. |> I am currently trying to keep one file open while opening other
  14. |> files one at a time using a separate file pointer.  However, as
  15. |> soon as I read a line from the second file, the first file pointer
  16. |> somehow gets destroyed and set to some position in the newly
  17. |> opened file.  Has anyone else encountered this?  And if so,
  18. |> is there a solution? (i.e. A way to protect the first file pointer
  19. |> from being overwritten.)
  20. |> 
  21. |> Here are the steps I am performing:
  22. |> 
  23. |> void main()
  24.  
  25. Surely you mean 'int main (void)'...
  26.  
  27. |> {
  28. |>     FILE *File1;
  29. |>     FILE *File2;
  30. |> 
  31. |>     File1 =   fopen("FILENAME", "r+");
  32.  
  33. Do you know that FILENAME was actually opened?
  34.  
  35. |>     /* loop through lines in File1 using fgets() */
  36. |> 
  37. |>     if (Condition) /* just indicating some condition was met */
  38. |>     {
  39. |>         File2 = fopen("FILENAME2", "r+");
  40.  
  41. Do you know that FILENAME2 was actually opened?
  42.  
  43. |>         fgets(Line, File2); <----- As soon as this occurs, File1 gets
  44. |>                                           wiped out.  Why?
  45.  
  46. Where is 'Line' declared? Also fgets() takes 3 parameters where you have given
  47. it only 2. In addition you never did an #include <stdio.h> to ensure proper
  48. prototypes for fgets() and fopen().
  49.  
  50. Therefore your program should not have even compiled.
  51.  
  52. |>     }
  53.  
  54.  
  55. Don't forget 'return (0);'
  56.  
  57. |> }
  58. -- 
  59. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  60.  
  61. Jonas J. Schlein  (schlein@gl.umbc.edu)
  62.